// 初始化开关状态
document.addEventListener('DOMContentLoaded', () => {
  const toggleSwitch = document.getElementById('popup-toggle');
  const optionsLink = document.getElementById('open-options');
  
  // 获取当前状态
  chrome.storage.sync.get(['hotkeyEnabled'], (result) => {
    toggleSwitch.checked = result.hotkeyEnabled !== false;
  });
  
  // 监听开关变化
  toggleSwitch.addEventListener('change', (e) => {
    const enabled = e.target.checked;
    
    // 更新存储状态
    chrome.storage.sync.set({ hotkeyEnabled: enabled }, () => {
      // 通知所有标签页更新状态
      chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
        if (tabs.length > 0) {
          chrome.tabs.sendMessage(tabs[0].id, { 
            action: 'setEnabledStatus', 
            enabled: enabled 
          });
        }
      });
    });
  });
  
  // 打开选项页面
  optionsLink.addEventListener('click', (e) => {
    e.preventDefault();
    chrome.runtime.openOptionsPage();
  });
});
